package com.atguigu.campusintelligentassistantsystem.controller;

import com.atguigu.campusintelligentassistantsystem.entity.ScholarshipApplication;
import com.atguigu.campusintelligentassistantsystem.entity.ScholarshipInfo;
import com.atguigu.campusintelligentassistantsystem.service.ScholarshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/scholarships")
@CrossOrigin(origins = "*")
public class ScholarshipController {

    @Autowired
    private ScholarshipService scholarshipService;

    /**
     * 发布奖学金信息
     * @param scholarshipInfo 奖学金信息
     * @return 保存后的奖学金信息
     */
    @PostMapping
    public ResponseEntity<ScholarshipInfo> publishScholarshipInfo(@RequestBody ScholarshipInfo scholarshipInfo) {
        ScholarshipInfo savedScholarshipInfo = scholarshipService.publishScholarshipInfo(scholarshipInfo);
        return ResponseEntity.ok(savedScholarshipInfo);
    }

    /**
     * 获取所有可申报的奖学金信息
     * @return 奖学金信息列表
     */
    @GetMapping
    public ResponseEntity<List<ScholarshipInfo>> getAvailableScholarships() {
        List<ScholarshipInfo> scholarships = scholarshipService.getAvailableScholarships();
        return ResponseEntity.ok(scholarships);
    }

    /**
     * 根据ID获取奖学金信息
     * @param id 奖学金ID
     * @return 奖学金信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<ScholarshipInfo> getScholarshipInfoById(@PathVariable Long id) {
        ScholarshipInfo scholarshipInfo = scholarshipService.getScholarshipInfoById(id);
        if (scholarshipInfo != null) {
            return ResponseEntity.ok(scholarshipInfo);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 更新奖学金信息
     * @param id 奖学金ID
     * @param scholarshipInfo 奖学金信息
     * @return 更新后的奖学金信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<ScholarshipInfo> updateScholarshipInfo(@PathVariable Long id, @RequestBody ScholarshipInfo scholarshipInfo) {
        scholarshipInfo.setId(id);
        ScholarshipInfo updatedScholarshipInfo = scholarshipService.updateScholarshipInfo(scholarshipInfo);
        return ResponseEntity.ok(updatedScholarshipInfo);
    }

    /**
     * 删除奖学金信息
     * @param id 奖学金ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteScholarshipInfo(@PathVariable Long id) {
        scholarshipService.deleteScholarshipInfo(id);
        return ResponseEntity.ok("奖学金信息删除成功");
    }

    /**
     * 提交奖学金申请
     * @param application 申请信息
     * @return 保存后的申请信息
     */
    @PostMapping("/applications")
    public ResponseEntity<ScholarshipApplication> submitApplication(@RequestBody ScholarshipApplication application) {
        ScholarshipApplication savedApplication = scholarshipService.submitApplication(application);
        return ResponseEntity.ok(savedApplication);
    }

    /**
     * 根据学生ID获取申请记录
     * @param studentId 学生ID
     * @return 申请记录列表
     */
    @GetMapping("/applications/student/{studentId}")
    public ResponseEntity<List<ScholarshipApplication>> getApplicationsByStudentId(@PathVariable Long studentId) {
        List<ScholarshipApplication> applications = scholarshipService.getApplicationsByStudentId(studentId);
        return ResponseEntity.ok(applications);
    }

    /**
     * 根据状态获取申请记录
     * @param status 状态
     * @return 申请记录列表
     */
    @GetMapping("/applications/status/{status}")
    public ResponseEntity<List<ScholarshipApplication>> getApplicationsByStatus(@PathVariable String status) {
        List<ScholarshipApplication> applications = scholarshipService.getApplicationsByStatus(status);
        return ResponseEntity.ok(applications);
    }

    /**
     * 更新申请状态和审核意见
     * @param id 申请ID
     * @param status 状态
     * @param comments 审核意见
     * @return 更新后的申请信息
     */
    @PutMapping("/applications/{id}/status")
    public ResponseEntity<ScholarshipApplication> updateApplicationStatus(@PathVariable Long id, 
                                                                         @RequestParam String status, 
                                                                         @RequestParam String comments) {
        ScholarshipApplication updatedApplication = scholarshipService.updateApplicationStatus(id, status, comments);
        if (updatedApplication != null) {
            return ResponseEntity.ok(updatedApplication);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 根据ID获取申请信息
     * @param id 申请ID
     * @return 申请信息
     */
    @GetMapping("/applications/{id}")
    public ResponseEntity<ScholarshipApplication> getApplicationById(@PathVariable Long id) {
        ScholarshipApplication application = scholarshipService.getApplicationById(id);
        if (application != null) {
            return ResponseEntity.ok(application);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}